import pygame, sys, os
from Constants import *
from Controls import *
import Sound, MenuHandler
import ClueGame, MapGame, PuzzleGame, EscapeGame


'''

make temp transitions

force one profile/data, always start at main menu transition





screen size
how to play with screen resolution with main, constants and config

no transition placeholders

tools? probably not enough time to rewrite
	replay

difficulty levels

newlevel and resetlevel format in games



add particles/spawns later
consider mouse, constants, levels, menus, options/config

'''


def main(*args):
	'''Do everything.'''
	#init everything
	pygame.init()

	camsurf = pygame.Surface(resolution)
	clock = pygame.time.Clock()

	sound = Sound.Sound()
	sound.unmute()
	sound.loadsounds()

	menuhandler = MenuHandler.MenuHandler()

	story = False

	wait_timer = 0

	#load configuration options
	try:
		pass
	except:
		pass

	#load saved data
	try:
		pass
	except:
		pass

#main menu loop
	while 1:
		delta = clock.tick(FPS)
		mouse1 = pygame.mouse.get_pos()

		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
			#elif event.type == pygame.VIDEORESIZE:
			else:
				menuhandler.handleevent(event)

#menu update logic
		menuhandler.update(False)
		if menuhandler.next_selection == QUIT:
			sys.exit()

#clear new events
		for e in new_events:
			new_events[e] = False

#draw menu
		camsurf.blit(black, (0,0))

		menuhandler.draw(camsurf)

		screen.blit(camsurf, (0,0))
		pygame.display.flip()

#start game check
		if menuhandler.next_selection < START_GAME:
			continue

		story = False
		if menuhandler.next_selection == START_GAME:
			menuhandler.next_selection = MAIN_MENU
			story = True
			game = ClueGame.ClueGame(resolution, (0,0), sound)
		elif menuhandler.next_selection == START_CLUE:
			game = ClueGame.ClueGame(resolution, (0,0), sound)
		elif menuhandler.next_selection == START_MAP:
			game = MapGame.MapGame(resolution, (0,0), sound)
		elif menuhandler.next_selection == START_PUZZLE:
			game = PuzzleGame.PuzzleGame(resolution, (0,0), sound)
		elif menuhandler.next_selection == START_ESCAPE:
			game = EscapeGame.EscapeGame(resolution, (0,0), sound)

#start game: could make this a function
		game.newlevel()

		game.setmouse(mouse1)

		if GRABEVENTS:
			pygame.event.set_grab(1)
		if HIDEMOUSE:
			pygame.mouse.set_visible(0)

		#change background music (in game)

#game loop
		while 1:

#pause menu
			if game.pause:
				delta = clock.tick(FPS)
				mouse1 = pygame.mouse.get_pos()

				#pause events
				for event in pygame.event.get():
					if event.type == pygame.QUIT:
						if game.forcequit():
							sys.exit()
					else:
						menuhandler.handleevent(event)

				#pause update
				menuhandler.update(True)
				if menuhandler.next_selection == START_GAME:
					#resume
					game.setpause(False)
					if GRABEVENTS:
						pygame.event.set_grab(1)
					if HIDEMOUSE:
						pygame.mouse.set_visible(0)
					delta = clock.tick()
				elif menuhandler.next_selection == RESET_GAME:
					#reset
					game.resetlevel()
					if GRABEVENTS:
						pygame.event.set_grab(1)
					if HIDEMOUSE:
						pygame.mouse.set_visible(0)
				elif menuhandler.next_selection == QUIT:
					menuhandler.next_selection = HUB_SCREEN
					game.quitgame()


#pause draw
				camsurf.blit(black, (0,0))
				camsurf.blit(game.camsurf, game.loc)

				menuhandler.draw(camsurf)
				#extras

				screen.blit(camsurf, (0,0))
				pygame.display.flip()

#continue game
			elif game.active:
				delta = clock.tick(FPS)
				if config.print_fps:
					print 1000./delta
				mouse1 = pygame.mouse.get_pos()
				game.mouse1 = game.convertmouse(mouse1)

				for event in pygame.event.get():
					if event.type == pygame.QUIT:
						if game.forcequit():
							sys.exit()
					#elif event.type == pygame.VIDEORESIZE:
					else:
						game.handleevent(event)

#update main and game
				if game.changemouse(mouse1):
					pygame.mouse.set_pos(game.unconvertmouse())

				game.update(delta)

#draw
				camsurf.blit(black, (0,0))
				game.draw(camsurf)

				screen.blit(camsurf, (0,0))
				pygame.display.flip()

#pre-pause
				if game.pause:
					menuhandler.activemenu = PAUSE_GAME
					menuhandler.next_selection = PAUSE_GAME
					if GRABEVENTS:
						pygame.event.set_grab(0)
					if HIDEMOUSE:
						pygame.mouse.set_visible(1)

			elif game.reset:
				delta = clock.tick(FPS)

				wait_timer += delta
				for event in pygame.event.get():
					if event.type == pygame.QUIT:
						sys.exit()
					game.handleevent(event)

				#reset after 2 seconds
				if wait_timer > 2000:
					wait_timer = 0

					#minor failure
					#else:#if game.lives > 0:
					game.resetlevel()

				screen.blit(camsurf, (0,0))
				pygame.display.flip()

#quit/win game
			else:
				#sound

				#go to next mini-game
				if game.wingame and story:
					menuhandler.next_selection += 1
					if menuhandler.next_selection == START_CLUE:
						game = ClueGame.ClueGame(resolution, (0,0), sound)
					elif menuhandler.next_selection == START_MAP:
						game = MapGame.MapGame(resolution, (0,0), sound)
					elif menuhandler.next_selection == START_PUZZLE:
						game = PuzzleGame.PuzzleGame(resolution, (0,0), sound)
					elif menuhandler.next_selection == START_ESCAPE:
						game = EscapeGame.EscapeGame(resolution, (0,0), sound)
					else:
						story = False

				if game.endgame or not story:
					story = False
					game.endgame = True
					menuhandler.activemenu = HUB_SCREEN
					menuhandler.next_selection = HUB_SCREEN
					break
				else:#story next game
					game.newlevel()
#clear new events
			for e in new_events:
				new_events[e] = False

if __name__ == "__main__":
	main(*sys.argv)
